package core

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

// UploadController 文件上传控制器
type UploadController struct {
	service UploadService
}

// Single godoc
// @Summary 单文件上传
// @Description 上传单个文件
// @Tags Upload
// @Accept  json
// @Produce  json
// @Param file formData file true "上传文件"
// @Success 200 {object} Upload
// @Failure 400 {object} httputil.HTTPError
// @Failure 401 {object} httputil.HTTPError
// @Failure 403 {object} httputil.HTTPError
// @Failure 404 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Router /core/upload [post]
func (c UploadController) Single(ctx *gin.Context) {
	if file, err := ctx.FormFile("file"); err != nil {
		ctx.JSON(http.StatusBadRequest, err)
	} else {
		if upload, err := c.service.Create(file, ctx); err != nil {
			ctx.JSON(http.StatusInternalServerError, err)
		} else {
			ctx.JSON(http.StatusOK, upload)
		}
	}
}

// Multi godoc
// @Summary 批量文件上传
// @Description 上传多个文件
// @Tags Upload
// @Accept  json
// @Produce  json
// @Param files formData file true "上传文件"
// @Success 200 {object} Upload
// @Failure 400 {object} httputil.HTTPError
// @Failure 401 {object} httputil.HTTPError
// @Failure 403 {object} httputil.HTTPError
// @Failure 404 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Router /core/upload_multi [post]
func (c UploadController) Multi(ctx *gin.Context) {
	if form, err := ctx.MultipartForm(); err != nil {
		ctx.JSON(http.StatusBadRequest, err)
	} else {

		if uploads, errs := c.service.CreateMulti(form.File["files[]"], ctx); len(errs) > 0 {
			ctx.JSON(http.StatusInternalServerError, errs)
		} else {
			ctx.JSON(http.StatusOK, uploads)
		}
	}
}
